<?php

// 上传游戏结果
require_once 'common/common.php';
require_once 'common/conn.php';

requestURL($_SERVER['REQUEST_URI']);

$result_array = array();

$detail = json_decode(getParameter("detail"), true);
if ($detail != "") {
    $result_array["errorNo"] = "0";
    $result_array["errorMsg"] = "";

    $usercode1 = $detail['user1'];
    $userid1 = getUseridByUsercode($usercode1);
    if ($userid1 == 0) {
        $result_array["errorNo"] = "1";
        $result_array["errorMsg"] = "UID:" . $usercode1 . " 不存在.";
    } else {
        $table1 = "t_game_detail_" . $userid1;
        if (!isTableExist($table1)) {
            createTable($table1);
        }
    }

    $usercode2 = $detail['user2'];
    if ($usercode2 != "aa") {
        $userid2 = getUseridByUsercode($usercode2);
        if ($userid2 == 0) {
            $result_array["errorNo"] = "1";
            $result_array["errorMsg"] = "UID:" . $usercode2 . " 不存在.";
        } else {
            $table2 = "t_game_detail_" . $userid2;
            if (!isTableExist($table2)) {
                createTable($table2);
            }
        }
    }

    if ($result_array['errorNo'] == "0") {
        $UID = $detail['UID'];
        $questions1 = $detail['questions1'];
        $allCorret = createRecord($table1, $UID, $usercode1, $usercode2, $questions1);
        if ($allCorret && isset($detail['type'])) {
            firstProfession($usercode1, $detail['type']);
            allAnswerRight($usercode1, $detail['type']);
        }
        userProperty($table1, $usercode1);
        costGold($table1, $usercode1);
        if ($usercode2 != "aa") {
            $questions2 = $detail['questions2'];
            $allCorret = createRecord($table2, $UID, $usercode2, $usercode1, $questions2);
            if ($allCorret && isset($detail['type'])) {
                firstProfession($usercode2, $detail['type']);
                allAnswerRight($usercode2, $detail['type']);
            }
            userProperty($table2, $usercode2);
            costGold($table2, $usercode2);
        }
    }
}
echo json_encode($result_array);

function getUseridByUsercode($usercode) {
    $db = new DBC();
    $sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "'";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        return $data[0]['deviceid'];
    }
    return 0;
}

function isTableExist($table) {
    $db = new DBC();
    $sql = "SHOW TABLES LIKE '" . $table . "'";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        return true;
    }
    return false;
}

function createTable($table) {
    $db = new DBC();
    $sql = "CREATE TABLE `" . $table . "` (
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            `usercode` varchar(50) DEFAULT NULL COMMENT 'uid,自动生成',
            `opponent` varchar(50) DEFAULT NULL COMMENT '对手',
            `uid` varchar(50) DEFAULT NULL COMMENT '局ID',
            `qid` varchar(10) DEFAULT NULL COMMENT '题目ID',
            `beginTime` datetime DEFAULT NULL COMMENT '答题开始时间',
            `endTime` datetime DEFAULT NULL COMMENT '答题结束时间',
            `correct` int(11) DEFAULT NULL COMMENT '1 正确 0 错误',
            `aProperty` varchar(10) DEFAULT 0 COMMENT '道具A 擦子',
            `bProperty` varchar(10) DEFAULT 0 COMMENT '道具B 加血',
            `cProperty` varchar(10) DEFAULT 0 COMMENT '道具C 冻结',
            `dProperty` varchar(10) DEFAULT 0 COMMENT '道具D 炸弹',
            `eProperty` varchar(10) DEFAULT 0 COMMENT '道具E 赞赏',
            `fProperty` varchar(10) DEFAULT 0 COMMENT '道具F 嘲讽',
            `createTime`datetime DEFAULT NULL COMMENT '创建时间',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
    return $db->execute($sql);
}

function createRecord($table, $UID, $user1, $user2, $questions) {
    //	public string strQuestionID;	public bool bCorrect;	public float fBeginTime;	public float fEndTime
    $allCorrect = true;
    $db = new DBC();
    if (checkArrayNotEmpty($questions)) {
        $total_e = 0;
        foreach ($questions as $q) {
            $correct = $q['bCorrect'] ? 1 : 0;
            $a = substr($q['strProperty'], 0, 1);
            $b = substr($q['strProperty'], 1, 1);
            $c = substr($q['strProperty'], 2, 1);
            $d = substr($q['strProperty'], 3, 1);
            $e = substr($q['strProperty'], 4, 1);
            $f = substr($q['strProperty'], 5, 1);
            $bTime = date("Y-m-d H:i:s", $q['fBeginTime']);
            $eTime = date("Y-m-d H:i:s", $q['fEndTime']);
            $sql = "INSERT INTO `" . $table . "` (usercode,opponent,uid,qid,beginTime,endTime,correct,aProperty,bProperty,cProperty,dProperty,eProperty,fProperty,createTime) VALUES('"
                    . $user1 . "','" . $user2 . "','" . $UID . "','" . $q['strQuestionID'] . "','" . $bTime . "','" . $eTime . "'," . $correct . ","
                    . $a . "," . $b . "," . $c . "," . $d . "," . $e . "," . $f . ",now())";
            $db->execute($sql);
            $total_e += $e;
            if (!$correct) {
                $allCorrect = false;
            }
        }
        
        //   achieve330	被别人赞美超过100次
        $sql = "UPDATE t_device SET praise = praise + " . $total_e . " WHERE devicecode = '" . $user2 . "'";
        $db->execute($sql);

        $sql = "SELECT achievecode FROM t_achieve_record WHERE usercode = '" . $user2 . "' AND achievecode = 'achieve330'";
        $data = $db->fetchData($sql);
        if (!checkArrayNotEmpty($data)) {
            $sql = "SELECT praise FROM t_device WHERE praise > 100";
            $data = $db->fetchData($sql);
            if (checkArrayNotEmpty($data)) {
                updateAchieve($user2, "achieve330");
            }
        }
    }
    $db->close();
    return $allCorrect;
}

function userProperty($table, $usercode) {
    $db = new DBC();
    $sql = "SELECT SUM(aProperty) AS a,SUM(bProperty) AS b,SUM(cProperty) AS c,SUM(dProperty) AS d,SUM(eProperty) AS e,SUM(fProperty) AS f FROM " . $table . " WHERE usercode = '" . $usercode . "'";
    $data = $db->fetchData($sql);
    // a擦子、b加血、c冻结、d炸弹、e赞赏、f嘲讽
    /*    achieve390	使用10次以上增加时间道具 c冻结
      achieve400	使用50次以上增加时间道具
      achieve410	使用10次以上增加生命道具  b加血
      achieve420	使用50次以上增加生命道具
      achieve430	使用10次以上炸弹道具   d炸弹
      achieve440	使用50次以上炸弹道具
      achieve450	使用10次以上橡皮擦道具  a擦子
      achieve460	使用50次以上橡皮擦道具

     */
    if (checkArrayNotEmpty($data)) {
        $a = $data[0]['a'];
        $b = $data[0]['b'];
        $c = $data[0]['c'];
        $d = $data[0]['d'];
        $e = $data[0]['e'];
        $f = $data[0]['f'];
        if ($f == 50) {
            updateAchieve($usercode, "achieve310");
        }
        if ($e == 50) {
            updateAchieve($usercode, "achieve320");
        }
        if ($c == 10) {
            updateAchieve($usercode, "achieve390");
        }
        if ($c == 50) {
            updateAchieve($usercode, "achieve400");
        }
        if ($b == 10) {
            updateAchieve($usercode, "achieve410");
        }
        if ($b == 50) {
            updateAchieve($usercode, "achieve420");
        }
        if ($d == 10) {
            updateAchieve($usercode, "achieve430");
        }
        if ($d == 50) {
            updateAchieve($usercode, "achieve440");
        }
        if ($a == 10) {
            updateAchieve($usercode, "achieve450");
        }
        if ($a == 50) {
            updateAchieve($usercode, "achieve460");
        }
    }
    $db->close();
}

function costGold($table, $usercode) {
    /*
     * 擦子：15、加血:30、冻结:10、炸弹:40、赞赏:2、嘲讽:2
      15, 30, 10, 40, 2, 2  分别对应道具的单价
     *  // a擦子、b加血、c冻结、d炸弹、e赞赏、f嘲讽
     * achieve360	购买道具花费超过500乐币
      achieve370	购买道具花费超过1000乐币
      achieve380	购买道具花费超过5000乐币
     */
    $db = new DBC();
    $sql = "SELECT achievecode FROM t_achieve_record WHERE usercode = '" . $usercode . "' AND (achievecode = 'achieve360' OR achievecode = 'achieve370' OR achievecode = 'achieve380')";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        $g5000 = false;
        $g1000 = false;
        $g500 = false;
        foreach ($data AS $code) {
            if ($code == "achieve380") {
                $g5000 = true;
            } else if ($code == "achieve370") {
                $g1000 = true;
            } else if ($code == "achieve360") {
                $g500 = true;
            }
        }
        if (!$g5000) {
            $sql = "SELECT SUM(aProperty) * 15 + SUM(bProperty) * 30 + SUM(cProperty) * 10 + SUM(dProperty) * 40 + SUM(eProperty) * 2 + SUM(fProperty) * 2 AS total FROM " . $table;
            $data = $db->fetchData($sql);
            if (checkArrayNotEmpty($data)) {
                $total = $data[0]['total'];
                if ($total > 5000) {
                    updateAchieve($usercode, "achieve380");
                } else if ($total > 1000 && !$g1000) {
                    updateAchieve($usercode, "achieve370");
                } else if ($total > 500 && !$g500) {
                    updateAchieve($usercode, "achieve360");
                }
            }
        }
    }
    $db->close();
}

/*
  achieve220	正确回答所有普通包的题目
  achieve230	正确回答所有动物包的题目
  achieve240	正确回答所有音乐包的题目
  achieve250	正确回答所有行为习惯包的题目
  achieve260	正确回答所有故事包的题目
  Pnomal，Panimal，Pmusic

 */

function allAnswerRight($usercode, $type) {/*
    $db = new DBC();
    if ($type == "Pnomal") {
        $sql = "SELECT achievecode FROM t_achieve_record WHERE usercode = '" . $usercode . "' AND achievecode = 'achieve220'";
        $data = $db->fetchData($sql);
        if (!checkArrayNotEmpty($data)) {
            updateAchieve($usercode, "achieve220");
        }
    } else if ($type == "Panimal") {
        $sql = "SELECT achievecode FROM t_achieve_record WHERE usercode = '" . $usercode . "' AND achievecode = 'achieve230'";
        $data = $db->fetchData($sql);
        if (!checkArrayNotEmpty($data)) {
            updateAchieve($usercode, "achieve230");
        }
    } else if ($type == "Pmusic") {
        $sql = "SELECT achievecode FROM t_achieve_record WHERE usercode = '" . $usercode . "' AND achievecode = 'achieve240'";
        $data = $db->fetchData($sql);
        if (!checkArrayNotEmpty($data)) {
            updateAchieve($usercode, "achieve240");
        }
    }
    $db->close();*/
}

function firstProfession($usercode, $type) {
    $db = new DBC();
    if ($type == "Panimal") {
        $sql = "SELECT achievecode FROM t_achieve_record WHERE usercode = '" . $usercode . "' AND achievecode = 'achieve350'";
        $data = $db->fetchData($sql);
        if (!checkArrayNotEmpty($data)) {
            updateAchieve($usercode, "achieve350");
        }
    } else if ($type == "Pmusic") {
        $sql = "SELECT achievecode FROM t_achieve_record WHERE usercode = '" . $usercode . "' AND achievecode = 'achieve350'";
        $data = $db->fetchData($sql);
        if (!checkArrayNotEmpty($data)) {
            updateAchieve($usercode, "achieve350");
        }
    }
    $db->close();
}

/*
 * achieve10	完成1局对战
  achieve20	完成50局对战
  achieve30	完成200局对战
  achieve40	完成1000局对战

  胜局
  achieve50	获得1次对战胜利
  achieve60	获得50次对战胜利
  achieve70	获得200次对战胜利
  achieve80	获得1000次对战胜利

  最大连续正确题数
  achieve120	连续正确30道题

  单局成就
  achieve130	在10回合时满血获得胜利
  achieve140	在10回合以内击倒对手
  achieve150	对战双方均无错题直到终局

  achieve160	只用2回合就获得对战胜利
  achieve170	在第10回合赢得胜利
  achieve180	在第10回合输掉游戏


  achieve190	一天内获得5次以上的对战胜利
  achieve200	一天内失败5局以上
  achieve210	一天内获得5次以上的平局

  按类型给大师：
  achieve220	正确回答所有普通包的题目
  achieve230	正确回答所有动物包的题目
  achieve240	正确回答所有音乐包的题目
  achieve250	正确回答所有行为习惯包的题目
  achieve260	正确回答所有故事包的题目

  暂不给编号
  暂不给编号
  achieve270	正确500道不同的题目
  achieve280	正确1000道不同的题目
  achieve290	错误500道不同的题目
  achieve300	错误1000道不同的题目

  achieve310	使用50次嘲讽道具
  achieve320	使用50次赞美道具
  achieve330	被别人赞美超过100次

  achieve340	一局游戏中购买道具花费超过70乐币

  achieve350	第一次使用专题题包对战

  achieve360	购买道具花费超过500乐币
  achieve370	购买道具花费超过1000乐币
  achieve380	购买道具花费超过5000乐币

  achieve390	使用10次以上增加时间道具
  achieve400	使用50次以上增加时间道具
  achieve410	使用10次以上增加生命道具
  achieve420	使用50次以上增加生命道具
  achieve430	使用10次以上炸弹道具
  achieve440	使用50次以上炸弹道具
  achieve450	使用10次以上橡皮擦道具
  achieve460	使用50次以上橡皮擦道具

  连败
  achieve470	连续失败5局

 */
?>
